#include "App_Modbus_Proto.h"
#include "App_Service_Task.h"
#include "ads1115-board.h"
#include "board.h"
#include "log.h"
#include "system.h"

void App_Service_Task(void* pvParameters)
{
    uint32_t times      = 0;
    uint16_t sampleTime = 0;
    uint8_t  i          = 0;
#if defined(CONFIG_DTU_485)
    SystemAttributeData_t appModbusProto = {0};
#endif

#if defined(CONFIG_DTU_AI)
    SystemLoRaData.aiChannelConfig[0] = ADS1115_CHANNEL1_CONFIG;
    SystemLoRaData.aiChannelConfig[1] = ADS1115_CHANNEL2_CONFIG;
    SystemLoRaData.aiChannelConfig[2] = ADS1115_CHANNEL3_CONFIG;
    SystemLoRaData.aiChannelConfig[3] = ADS1115_CHANNEL4_CONFIG;
#endif

    while (1) {
        times++;
        // LOG_PRINTF(LL_DEBUG,"Srv_Services_Task,FreeHeapSize=%d\r\n",xPortGetFreeHeapSize());

#if defined(CONFIG_DTU_485)
        for (i = 0; i < SYSTEM_ATTRIBUTE_NUM; i++) {
            if (SystemParmData.attributeData[i].attrId != 0) {
                // sampleTime = SystemParmData.attributeData[i].sampleTime;
                // if((times * APP_Service_TASK_DELAY_MS)%(sampleTime*1000) == 0)
                // {
                // memset(&appModbusProto,0,sizeof(appModbusProto));
                // appModbusProto.attrId = SystemParmData.attributeData[i].attrId;
                // appModbusProto.slaveAddr = SystemParmData.attributeData[i].attrSlaveAddr;
                // appModbusProto.cmd = SystemParmData.attributeData[i].attrCmd;
                // appModbusProto.regaddr = SystemParmData.attributeData[i].attrRegaddr;
                // appModbusProto.regnum  = SystemParmData.attributeData[i].attrRegnum;

                // appModbusProto.resultOffset = SystemParmData.attributeData[i].attrResultOffset;	//取返回值的偏移量；
                // appModbusProto.resultType = SystemParmData.attributeData[i].attrResultType;

                // uint8_t out[64] = {0};
                // int len = AppSerialTask(&appModbusProto);

                // xQueueSend(SystemLoRaData.rs485TxQueue,&appModbusProto,portMAX_DELAY);
                // }
            }
        }
        SystemLoRaData.systemState.flagBit.rs485SampleFirst = 1;
#endif

#if defined(CONFIG_DTU_AI)
        for (i = 0; i < SYSTEM_AI_ATTRIBUTE_NUM; i++) {
            if (SystemParmData.aiAttrData[i].attrId != 0) {
                sampleTime = SystemParmData.aiAttrData[i].sampleTime;
                if ((times * APP_Service_TASK_DELAY_MS) % (sampleTime * 1000) == 0) {
                    SystemLoRaData.aiAttrSampleData[i].attrData = BoardAds1115Result(SystemLoRaData.aiChannelConfig[i]);
                }
            }
        }
        SystemLoRaData.systemState.flagBit.aiSampleFirst = 1;
#endif

#if defined(CONFIG_DTU_DOUT)
        for (i = 0; i < SYSTEM_DOUT_ATTRIBUTE_NUM; i++) {
            if (SystemParmData.doutAttrData[i].attrId != 0) {
                SystemDoutModeSet(SystemParmData.doutAttrData[i].attrId, SystemParmData.doutAttrData[i].attrData);
            }
        }
#endif

#if defined(CONFIG_DTU_DIN)
        for (i = 0; i < SYSTEM_DIN_ATTRIBUTE_NUM; i++) {
            if (SystemParmData.dinAttrData[i].attrId != 0) {
                SystemLoRaData.dinAttrSampleData[i].attrData = SystemDinModeGet(SystemParmData.dinAttrData[i].attrId);
            }
        }
        SystemLoRaData.systemState.flagBit.dinSampleFirst = 1;
#endif

        vTaskDelay(APP_Service_TASK_DELAY_MS);
    }
}
